<template>
  <!--index.wxml-->
  <view class="container">
    <view class="background-image"></view>
    <view class="bg-sear">
      <view class="scrolltop">
        <view class="section" @tap="toSearchPage">
          <image src="/static/images/icon/search.png" class="search-img"></image>
          <text class="placeholder">请输入关键词搜索</text>
        </view>
      </view>
    </view>

    <!-- swiper -->
    <swiper
        :autoplay="autoplay"
        :indicator-color="indicatorColor"
        :interval="interval"
        :duration="duration"
        :indicator-active-color="indicatorActiveColor + ' '"
        circular="true"
        class="pic-swiper"
        indicator-dots
    >
      <block v-for="(item, index) in indexImgs" :key="index">
        <swiper-item class="banner-item">
          <view class="img-box">
            <image :src="item.imgUrl" :data-prodid="item.relation" @tap="toProdPage" class="banner"></image>
          </view>
        </swiper-item>
      </block>
    </swiper>
    <!-- end swiper -->

    <view class="content">

      <view class="cat-item">
        <view class="item" @tap="toClassifyPage" data-sts="0" data-id="2">
          <image src="/static/images/icon/timePrice.png"></image>
          <text>热门榜单</text>
        </view>
        <view class="item" @tap="toClassifyPage" data-sts="0" data-id="1">
          <image src="/static/images/icon/newProd.png"></image>
          <text>新品首发</text>
        </view>
        <view class="item" @tap="toCategoryPage">
          <image src="/static/images/icon/neweveryday.png"></image>
          <text>逛一逛</text>
        </view>
        <view class="item" @tap="toCouponCenter">
          <image src="/static/images/icon/newprods.png"></image>
          <text>特价推荐</text>
        </view>
        <view class="item" @tap="toCouponCenter">
          <image src="/static/images/icon/oil-cards.png"></image>
          <text>油卡直充</text>
        </view>
      </view>

      <!-- 消息播放 -->
      <!--    <view v-if="news && news.length" class="message-play" @tap="onNewsPage">-->
      <!--      <image src="/static/images/icon/horn.png" class="hornpng"></image>-->
      <!--      <swiper :vertical="true" :autoplay="true" :circular="true" duration="1000" class="swiper-cont">-->
      <!--        <block v-for="(item, index) in news" :key="index">-->
      <!--          <swiper-item class="items">{{item.title}}</swiper-item>-->
      <!--        </block>-->
      <!--      </swiper>-->
      <!--      <text class="arrow"></text>-->
      <!--    </view>-->

    </view>

    <view class="updata" v-if="updata">
      <block v-for="(item, index) in taglist" :key="index">
        <!-- 10-29积分专区 -->
        <view class="up-to-date" v-if="item.id === 1 && item.prods && item.prods.length > 0">
          <view class="title">
            <view class="main-title">
              <image class="tag-icon" src="/static/images/icon/10-29-icon.png"></image>
              <image class="tag-font" src="/static/images/icon/10-29-font.png"></image>
            </view>
            <view class="more-prod-cont" @tap="toClassifyPage" data-sts="1">
              <text class="more">查看更多></text>
            </view>
          </view>
          <view class="item-cont">
            <block v-for="(prod, index2) in item.prods" :key="index2">
              <view class="prod-item" @tap="toProdPage" :data-prodid="prod.prodId">
                <view class="imagecont">
                  <image :src="prod.pic" class="prodimg"></image>
                </view>
                <view class="prod-text">{{ prod.prodName }}</view>
                <view class="sale-text">已累计兑换:{{ prod.orderTotal }}</view>
                <view class="item-bottom">
                  <view class="prod-price">
                    <text class="symbol">￥</text>
                    <text class="big-num">{{ wxs.parsePrice(prod.price)[0] }}</text>
                    <text class="small-num">.{{ wxs.parsePrice(prod.price)[1] }}</text>
                  </view>
                  <view class="tag-hint">
                    <image class="tag-hint-img" src="/static/images/icon/10-29-basket.png"></image>
                  </view>
                </view>
              </view>
            </block>
          </view>
        </view>

        <!-- 30-49积分专区 -->
        <view class="hot-sale" v-if="item.id === 2 && item.prods && item.prods.length > 0">
          <view class="title">
            <view class="main-title">
              <image class="tag-icon" src="/static/images/icon/30-49-icon.png"></image>
              <image class="tag-font" src="/static/images/icon/30-49-font.png"></image>
            </view>
            <view class="more-prod-cont" @tap="toClassifyPage" data-sts="2">
              <text class="more">查看更多></text>
            </view>
          </view>
          <view class="item-cont">
            <block v-for="(prod, index2) in item.prods" :key="index2">
              <view class="prod-item" @tap="toProdPage" :data-prodid="prod.prodId">
                <view class="imagecont">
                  <image :src="prod.pic" class="prodimg"></image>
                </view>
                <view class="prod-text">{{ prod.prodName }}</view>
                <view class="sale-text">已累计兑换:{{ prod.orderTotal }}</view>
                <view class="item-bottom">
                  <view class="prod-price">
                    <text class="symbol">￥</text>
                    <text class="big-num">{{ wxs.parsePrice(prod.price)[0] }}</text>
                    <text class="small-num">.{{ wxs.parsePrice(prod.price)[1] }}</text>
                  </view>
                  <view class="tag-hint">
                    <image class="tag-hint-img" src="/static/images/icon/30-49-basket.png"></image>
                  </view>
                </view>
              </view>
            </block>
          </view>
        </view>

        <!-- 50积分以上专区 -->
        <view class="up-sale" v-if="item.id === 3 && item.prods && item.prods.length > 0">
          <view class="title">
            <view class="main-title">
              <image class="tag-icon" src="/static/images/icon/50-icon.png"></image>
              <image class="tag-font" src="/static/images/icon/50-font.png"></image>
            </view>
            <view class="more-prod-cont" @tap="toClassifyPage" data-sts="3">
              <text class="more">查看更多></text>
            </view>
          </view>
          <view class="item-cont">
            <block v-for="(prod, index2) in item.prods" :key="index2">
              <view class="prod-item" @tap="toProdPage" :data-prodid="prod.prodId">
                <view class="imagecont">
                  <image :src="prod.pic" class="prodimg"></image>
                </view>
                <view class="prod-text">{{ prod.prodName }}</view>
                <view class="sale-text">已累计兑换:{{ prod.orderTotal }}</view>
                <view class="item-bottom">
                  <view class="prod-price">
                    <text class="symbol">￥</text>
                    <text class="big-num">{{ wxs.parsePrice(prod.price)[0] }}</text>
                    <text class="small-num">.{{ wxs.parsePrice(prod.price)[1] }}</text>
                  </view>
                  <view class="tag-hint">
                    <image class="tag-hint-img" src="/static/images/icon/50-basket.png"></image>
                  </view>
                </view>
              </view>
            </block>
          </view>
        </view>

        <!-- 更多宝贝 -->
        <view class="more-prod" v-if="item.id === 4 && item.prods && item.prods.length > 0">
          <view class="title">
              <image class="tag-icon" src="/static/images/icon/more-prods-icon.png"></image>
              <image class="tag-font" src="/static/images/icon/more-prods-font.png"></image>
          </view>
          <view class="item-cont">
            <block v-for="(prod, index2) in item.prods" :key="index2">
              <view class="prod-item" @tap="toProdPage" :data-prodid="prod.prodId">
                <view class="prod-item-left">
                    <image :src="prod.pic"></image>
                </view>
                <view class="prod-item-right">
                  <view class="prod-text">{{ prod.prodName }}</view>
                  <view class="sale-text">已累计兑换:{{ prod.orderTotal }}</view>
                  <view class="prod-price">
                    <text class="symbol">￥</text>
                    <text class="big-num">{{ wxs.parsePrice(prod.price)[0] }}</text>
                    <text class="small-num">.{{ wxs.parsePrice(prod.price)[1] }}</text>
                  </view>
                  <view class="tag-hint">
                    <image class="tag-hint-img" src="/static/images/icon/10-29-basket.png"></image>
                  </view>
                </view>
              </view>
            </block>
          </view>
          <view class="more-bottom" @tap="getTagProdForAppend">
            <view class="divide-left"></view>
            <view class="text">查看更多></view>
            <view class="divide-right"></view>
          </view>
          <!-- <view class="service" @tap="toCustomerService"> -->
        </view>
      </block>
    </view>
    <view class="service" >
      <a :href="customerServ">
        <image src="/static/images/icon/customer-service.png"></image>
      </a>
    </view>
  </view>
</template>

<script module="wxs" lang="wxs" src="../../wxs/number.wxs"></script>

<script>
//index.js
//获取应用实例
var http = require("../../utils/http.js");
var config = require("../../utils/config.js");
const app = getApp();

export default {
  data() {
    return {
      pages: 0,
      lastIndex: 1,
      indicatorDots: true,
      indicatorColor: '#d1e5fb',
      indicatorActiveColor: '#1b7dec',
      autoplay: true,
      interval: 2000,
      duration: 1000,
      indexImgs: [],
      seq: 0,
      news: [],
      sts: 0,
      scrollTop: "",
      current: 0,
      updata: true,
	  customerServ: '',
      taglist: [
        {id: 1, url: "/prod/pageByPrice1", data: {current: 1, size: 4}},
        {id: 2, url: "/prod/pageByPrice2", data: {current: 1, size: 4}},
        {id: 3, url: "/prod/pageByPrice3", data: {current: 1, size: 4}},
        {id: 4, url: "/prod/prodListByTagId", data: {tagId: 3, size: 4}}
      ],
      moreProds: []
    };
  },

  components: {},
  props: {},
  onLoad: function () {
    console.log("=============onLoad==============")
    let code = this.GetQueryString("code")
	let paramMallId=this.GetQueryString("mallid")


    
	if(code != null && code != ""){
		
			
		  var params = {
			
			url: "/user/wxlogin2023/" + code,
			method: "POST",
			data: {
				host: window.location.host,
				mallid: paramMallId
			},

			callBack: res => {
			  if (res.fail) {
				uni.showModal({
				  title: "错误",
				  showCancel: false,
				  
				  content: res.msg,
				  
				  confirmText: "确定",
				  success: res => {

				  }
				})
			  } else {
				  uni.setNavigationBarTitle({
				      title: res.mallName
				  })
				  
				  // 改变全局变量
				  console.log("----------",getApp().globalData.myTitle,getApp().globalData.myAppId,getApp().globalData.customerServ)
				  getApp().globalData.myTitle = res.mallName
				  getApp().globalData.myAppId=res.wxAppId
				  getApp().globalData.customerServ=res.customerServ
				  this.customerServ=getApp().globalData.customerServ	
				  uni.setStorageSync("customerServ", res.customerServ)
				  uni.setStorageSync("mallName", res.mallName)
				  console.log("----------",getApp().globalData.myTitle,getApp().globalData.myAppId,getApp().globalData.customerServ)
				  let mallType=res.mallType
				  let phone=res.phone
				http.loginSuccess(res, () => {
					if(mallType=='1' && (phone==null || phone=='') ){
						uni.navigateTo({
							url: '/pages/binding-phone/binding-phone'
						});
					}else{
						uni.switchTab({
							url: '/pages/index/index'
						});
					}
				  
				})
			  }
			},
			errCallBack: res => {


			  uni.showModal({
				title: "错误",
				showCancel: false,
				content: "无法获取微信用户信息，请在公众号中点击菜单进入商城",
				
				confirmText: "确定",
				success: res => {

				}
			  })

			}
		  };

		  http.request(params);

		
	}else{
	
		uni.setNavigationBarTitle({
			title: uni.getStorageSync("mallName")
		})	
		this.customerServ=uni.getStorageSync("customerServ")	
		console.log("customerServ",this.customerServ)
	}
    this.getAllData();
	
	
	
  },
  onShow: function () {

    http.getCartCount(); //重新计算购物车总数量
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log("onReachBottom");
    // this.getTagProdForAppend();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {
  //     wx.request({
  //       url: '',
  //       data: {},
  //       method: 'GET',
  //       success: function (res) { },
  //       fail: function (res) { },
  //       complete: function (res) {
  //         wx.stopPullDownRefresh();
  //       }
  //     })
  // },
  onPullDownRefresh: function () {
    // wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    var ths = this;
    setTimeout(function () {
      ths.getAllData(); // wx.hideNavigationBarLoading() //完成停止加载

      uni.stopPullDownRefresh(); //停止下拉刷新
    }, 100);
  },
  methods: {
    GetQueryString: function (name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
      var context = "";
      if (r != null)
        context = decodeURIComponent(r[2]);
      reg = null;
      r = null;
      return context == null || context == "" || context == "undefined" ? "" : context;
    },
    //事件处理函数
    bindViewTap: function () {
      uni.navigateTo({
        url: '../logs/logs'
      });
    },
    // 页面滚动到指定位置指定元素固定在顶部
    onPageScroll: function (e) {
      //监听页面滚动
      this.setData({
        scrollTop: e.scrollTop
      });
    },
    toProdPage: function (e) {
      var prodid = e.currentTarget.dataset.prodid;

      if (prodid) {
        uni.navigateTo({
          url: '/pages/prod/prod?prodid=' + prodid
        });
      }
    },
    // 加入购物车
    addToCart: function (item) {
      uni.showLoading({
        mask: true
      });
      var params = {
        url: "/prod/prodInfo",
        method: "GET",
        data: {
          prodId: item.prodId
        },
        callBack: res => {
          var params1 = {
            url: "/p/shopCart/changeItem",
            method: "POST",
            data: {
              basketId: 0,
              count: 1,
              prodId: res.prodId,
              shopId: res.shopId,
              skuId: res.skuList[0].skuId
            },
            callBack: res => {
              //console.log(res);
              uni.hideLoading();
              http.getCartCount(); //重新计算购物车总数量
              uni.showToast({
                title: "加入购物车成功",
                icon: "none"
              });
            }
          };
          http.request(params1);
        }
      };
      http.request(params);
    },
    toCouponCenter: function () {
      uni.showToast({
        icon: "none",
        title: '商品上架中，敬请期待！！'
      });
    },
    // 跳转搜索页
    toCategoryPage: function () {
      uni.switchTab({
        url: '/pages/category/category'
      });
    },
    // 跳转搜索页
    toSearchPage: function () {
      uni.navigateTo({
        url: '/pages/search-page/search-page'
      });
    },
    //跳转商品活动页面
    toClassifyPage: function (e) {
      var url = '/pages/prod-classify/prod-classify?sts=' + e.currentTarget.dataset.sts;
      var id = e.currentTarget.dataset.id;
      var title = e.currentTarget.dataset.title;

      if (id) {
        url += "&tagid=" + id + "&title=" + title;
      }

      uni.navigateTo({
        url: url
      });
    },
    //跳转公告列表页面
    onNewsPage: function () {
      uni.navigateTo({
        url: '/pages/recent-news/recent-news'
      });
    },

    getAllData() {
      http.getCartCount(); //重新计算购物车总数量

      this.getIndexImgs();
      this.getNoticeList();
      this.getTag();
    },

    //加载轮播图
    getIndexImgs() {
      //加载轮播图
      var params = {
        url: "/indexImgs",
        method: "GET",
        data: {},
        callBack: res => {
          this.setData({
            indexImgs: res,
            seq: res
          });
        }
      };
      http.request(params);
    },

    getNoticeList() {
      // 加载公告
      var params = {
        url: "/shop/notice/topNoticeList",
        method: "GET",
        data: {},
        callBack: res => {
          this.setData({
            news: res
          });
        }
      };
      http.request(params);
    },

    // 加载商品标题分组列表
    getTag() {
      for (var i = 0; i < this.taglist.length; i++) {
        this.updata = false
        this.updata = true
        this.getTagProd(this.taglist[i]);
      }
    },

    getTagProd(tag) {
      var param = {
        url: tag.url,
        method: "GET",
        data: tag.data,
        callBack: res => {
          this.updata = false
          this.updata = true
          var taglist = this.taglist;
          taglist[tag.id - 1].prods = res.records;
          this.setData({
            taglist: taglist
          });
        }
      };
      http.request(param);
    },

    getTagProdForAppend() {
      var param = {
        url: "/prod/prodListByTagId",
        method: "GET",
        data: {
          tagId: 3,
          current: ++this.lastIndex,
          size: 4
        },
        callBack: res => {
          this.updata = false
          this.updata = true
          var taglist = this.taglist;
          taglist[3].prods = taglist[3].prods.concat(res.records);
          this.setData({
            taglist: taglist
          });

        }
      };
      http.request(param);
    },

    /**
     * 跳转至商品详情
     */
    showProdInfo: function (e) {
      let relation = e.currentTarget.dataset.relation;

      if (relation) {
        uni.navigateTo({
          url: 'pages/prod/prod'
        });
      }
    }
  }
};
</script>
<style>
@import "./index.css";
</style>
